<?php

class Core_Agent_Component_Subject_Controller_Concessions extends Dcmp_Component_Controller {


    public function createAction(){
        $category = App::getModel('promotion/collection_concessions_category')->load();
        $pois = App::getModel('foundation/collection_poi')
                ->joinAccount()
                ->filterBy('accoundid',$this->getIdentity()->getId())
                ->load();
        $themes = App::getModel('design/collection_page_theme')
                        ->filterByType('concessions')
                        ->filterByEnabled(1)
                        ->load();
        Kd_Web::getPage()->getNavigation()
                ->addLink($this->getSession()->get('current_subject_name'),$this->getUrl(NULL,'index','subject'))
                ->addLabel('创建优惠');
        return $this->viewResult(array('category'=>$category,'pois'=>$pois,'themes'=>$themes));
    }

    public function editAction($id){
        $model = App::getModel('promotion/collection_concessions')->joinSubject()->filterBy('id',$id)->load();
        $subject = App::getModel('subject/content')->load($model[0]['subject_id']);
        $category = App::getModel('promotion/collection_concessions_category')->load();
        $pois = App::getModel('foundation/collection_poi')
                ->joinAccount()
                ->filterBy('accoundid',$this->getIdentity()->getId())
                ->load();
        $themes = App::getModel('design/collection_page_theme')
                        ->filterByType('concessions')
                        ->filterByEnabled(1)
                        ->load();
        Kd_Web::getPage()->getNavigation()
                ->addLink($this->getSession()->get('current_subject_name'),$this->getUrl(NULL,'index','subject'))
                ->addLink('优惠列表',$this->getUrl($this->getSession()->get('current_subject_id'),'index','concessions'))
                ->addLabel($model[0]->getName());
        return $this->viewResult(array('category'=>$category,'pois'=>$pois,'themes'=>$themes,'model'=>$model->getFirstItem()),'create');
    }

    public function savePostAction($state) {
        if ($this->saveModel($state, $model)) {
            return $this->redirectResult($this->getUrl(array('subject_id'=>$this->getSession()->get('current_subject_id')),'index'));
        }
        return $this->createAction()->setModel($model);
    }

    private function saveModel($state, &$savedModel = NULL) {
        $pictureName = array();
        $data = $this->getRequest()->getForm('concessions');
        $subject = App::getModel('subject/content')->load($this->getSession()->get('current_subject_id'));
        $model = App::getModel('promotion/concessions');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
            $subject->updateConcessionsRelation($data);
        }
        $model->addData($data)->setStatus($state)->setOwnerId($this->getIdentity()->getId());
        foreach($data['picture'] as $picture){
            $pictureName[] = basename($picture);
        }
        $model->setPicture(implode(',', $pictureName));
        $savedModel = $model;
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            Kd_Util_IO::moveFiles(Kd_Web_Util::urlsToPaths($data['picture']),
            					  DP_PUB.'concessions'.DS.'content'.DS.$model->getId(),True);
            if (empty($data['id'])) {
                $subject->associateConcessions($model);
            }
            $this->getMessager()->addInform($this->__('share.message.success'));
             return TRUE;
             }
            else {
            $this->getMessager()->addError($errors);
            return FALSE;
        }
    }

    public function indexAction($subject_id,$page=1,$sort = NULL){
        $subject = App::getModel('subject/content')->load($subject_id);
        $concessions = $subject->getConcessions()
                            ->filterBy('ownerid',$this->getIdentity()->getId())
                            ->pagination($page, 10);
        if(!is_null($sort)){
            $concessions->sortBy($sort,'desc');
        }
        $concessions->load();
        $records = $concessions->loadCount();
        $args = array_merge($this->getRequest()->getAllParameters(), array('page' => '{index}'));
        $pagination = new Kd_Html_Pagination();
        $pagination->setPageIndex($page)
                   ->setPageSize(10)
                   ->setPageRange(6)
                   ->setRecordCount($records)
                   ->setUrlFormat($this->getUrl($args));
        Kd_Web::getPage()->getNavigation()
                ->addLink($subject->getName(),$this->getUrl(NULL,'index','subject'))
                ->addLabel('优惠列表');
        Kd_Web::getPage()->setTitle('优惠列表')->setKeyword('');
        $this->getSession()->set('current_subject_id',$subject_id);
        $this->getSession()->set('current_subject_name',$subject->getName());
        $this->getVariables()
                ->setData('concessions_list', $concessions)
                ->setData('concessions_list_count', $records)
                ->setData('concessions_list_page_index', $page)
                ->setData('concessions_list_page_size', 10)
                ->setData('concessions_list_pagination', $pagination);
        return $this->viewResult(array('concessions'=>$concessions,'pagination'=>$pagination));
    }

    public function previewAction($id){
        $this->getSession()->set('is_concessions_preview','a');
        return $this->redirectResult('/concessions/content/detail/id/'.$id);
    }

}